﻿using System;
using System.Collections;

namespace Grebok.Persistence
{
    class IdentityMap
    {
        private Hashtable mappedTypes;

        public IdentityMap()
        {
            mappedTypes = new Hashtable();
        }

        public object GetObject(Type objectType, object key)
        {
            Hashtable map = GetMapFromType(objectType);
            object item = map[key.ToString()];
            return item;
        }

        public void PutObject(object item, object key)
        {
            Type objectType = item.GetType();
            Hashtable map = GetMapFromType(objectType);
            map[key.ToString()] = item;
        }

        private Hashtable GetMapFromType(Type objectType)
        {
            string typeName = objectType.FullName;
            Hashtable map = GetMap(typeName);
            return map;
        }

        private Hashtable GetMap(string mapName)
        {
            Hashtable map = (Hashtable)mappedTypes[mapName];
            if (map == null)
            {
                Hashtable newMap = new Hashtable();
                mappedTypes[mapName] = newMap;
                map = newMap;
            }
            return map;
        }

        private enum ObjectState
        {
            Created,
            Deleted,
            Updated
        }

        private class MappedItem
        {
            private ObjectState state;
            private object item;

            public ObjectState State
            {
                get { return state; }
                set { state = value; }
            }

            public object Item
            {
                get { return item; }
                set { item = value; }
            }
        }
    }
}
